package mybatis;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Objects;
import java.io.PrintWriter;

public class Mylinker implements SqlUtil{
	
	@Override
	public String query(Class<?> clazz) {
        StringBuilder str = new StringBuilder();
        String tableName = getTableName(clazz);
        str.append("select * from ").append(tableName);
        String resultStr = str.toString();
        return resultStr;
    }

	@Override
    public String delete(Class<?> clazz, Object object) {
        StringBuilder str = new StringBuilder();
        String tableName = getTableName(clazz);
        str.append("delete from ").append(tableName).append(" where ");
        Field[] fields = clazz.getDeclaredFields();
        String pk = "";
        for (int i = 0; i < fields.length; i++) {
            Column column = fields[i].getAnnotation(Column.class);
            if (column.isPK()) {
                pk = column.label();
                break;
            }
        }
        try {
            str.append(pk).append(" = ").append(getValue(pk, object));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String resultStr = str.toString();
        return resultStr;
    }


    @Override
    public String insert(Class<?> clazz, Object object) {
        StringBuilder str = new StringBuilder();
        String tableName = getTableName(clazz);
        str.append("insert into ").append(tableName).append(" (");
        Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length - 1; i++) {
            Column column = fields[i].getAnnotation(Column.class);
            str.append(column.label()).append(" , ");
        }
        Column columns = fields[fields.length - 1].getAnnotation(Column.class);
        str.append(columns.label()).append(") values(");
        try {
            for (int i = 0; i < fields.length - 1; i++) {
                Column column = fields[i].getAnnotation(Column.class);
                str.append(getValue(column.label(), object)).append(" , ");
            }
            str.append(getValue(columns.label(),object)).append(")");
        } catch (Exception e) {
            e.printStackTrace();
        }


        String resultStr = str.toString();
        return resultStr;
    }

    @Override
    public String update(Class<?> clazz, Object object) {
        StringBuilder str = new StringBuilder();
        String tableName = getTableName(clazz);
        str.append("update ").append(tableName).append(" set ");
        Field[] fields = clazz.getDeclaredFields();
        String pk = "";
        try {
            for (int i = 0; i < fields.length - 1; i++) {
                Column column = fields[i].getAnnotation(Column.class);
                if (column.isPK()) {
                    pk = column.label();
                } else {

                    str.append(column.label()).append(" = ").append(getValue(column.label(), object)).append(",");

                }
            }
            Column columns = fields[fields.length - 1].getAnnotation(Column.class);
            str.append(columns.label()).append(" = ").append(getValue(columns.label(), object)).append(" ");
            str.append(" where ").append(pk).append(" = ").append(getValue(pk, object));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String resultStr = str.toString();
        return resultStr;
    }


    //获取表的名称
    private String getTableName(Class<?> clazz) {
        //判断是否为Table注释类型是方法返回true，否则返回false
        if (clazz.isAnnotationPresent(Table.class)) {
            //获取注解信息
            Table table = clazz.getAnnotation(Table.class);
            if (!"".equals(table.tableName())) {
                return table.tableName();
            }
        }
        return null;
    }

    //通过对象的值，来获取实际的值
    private <T> String getValue(String columnName, Object object) throws Exception {
        //获取相应字段的getXXX方法
        String getMethod = columnName.substring(0, 1).toUpperCase() + columnName.substring(1);
        Method method = object.getClass().getMethod("get" + getMethod);
        return method.invoke(object).toString();
    }

    
}

